<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="5" :xs="24">
        <el-card shadow="never">
          <org-tree ref="orgTree" modelType="'dept','community'" @nodeClick="handleNodeClick" @firstNodeClick="handleFirstNodeClick"/>
        </el-card>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-card shadow="never">
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="政策名称" prop="favouredPolicyName">
              <el-input
                v-model="queryParams.favouredPolicyName"
                placeholder="请输入政策名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="优惠方式" prop="preferentialMethod">
              <el-select v-model="queryParams.preferentialMethod" placeholder="请选择优惠方式" clearable size="small">
                <el-option
                  v-for="dict in preferentialMethodOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="政策生效时间" prop="beginTime">
              <el-date-picker
                v-model="effectiveTime"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['payfee:favouredPolicy:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['payfee:favouredPolicy:edit']"
              >修改</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="favouredPolicyList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="政策名称" align="center" prop="favouredPolicyName" />
            <el-table-column label="适用标准" align="center" prop="normId" width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{getNormNames(scope.row)}}
              </template>
            </el-table-column>
            <el-table-column label="政策生效时间" align="center" width="200">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
                至
                <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="优惠方式" align="center" prop="preferentialMethod" :formatter="preferentialMethodFormat" />
            <el-table-column label="政策简介" align="center" prop="synopsis" />
            <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" width="120" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
              <template slot-scope="scope">
            <!--    <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['payfee:favouredPolicy:edit']"
                >修改</el-button>-->
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleStart(scope.row)"
                  v-if="scope.row.status=='101'|| scope.row.status=='103'"
                  v-hasPermi="['payfee:favouredPolicy:edit']"
                >启用</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleEnd(scope.row)"
                  v-if="scope.row.status=='102'"
                  v-hasPermi="['payfee:favouredPolicy:edit']"
                >禁用</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>
    <favoured-policy-form ref="policyFormRef" @ok="this.getList"/>
  </div>
</template>

<script>
  import { listFavouredPolicy, delFavouredPolicy , exportFavouredPolicy ,updateStatus} from "@/api/payfee/favouredPolicy";
  import { policyMinxin } from "./policyMinxin.js";
  import FavouredPolicyForm from "./policyForm";
  export default {
    name: "FavouredPolicy",
    components:{FavouredPolicyForm},
    mixins: [ policyMinxin ],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 优惠政策表格数据
        favouredPolicyList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          favouredPolicyName: null,
          communityId: null,
          projectId: null,
          chargeId: null,
          normId: null,
          beginTime: null,
          finishTime: null,
          preferentialMethod: null,
          wipeOff: null,
          sharingMethod: null,
          preferentialType: null,
          discountMethod: null,
          reliefMethod: null,
          largessMethod: null,
          synopsis: null,
          systemType: null,
          status: null,
        },
        //收费标准字典
        listNormOptions:[],
        //政策生效时间
        effectiveTime:[]
      };
    },
    created() {

    },
    methods: {
      /** 查询优惠政策列表 */
      getList() {
        this.loading = true;
        listFavouredPolicy(this.queryParams).then(response => {
          this.favouredPolicyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.effectiveTime = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.favouredPolicyId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        if(this.currentNode.modelType!= 'community'){
          this.msgError("请选择小区");
          return;
        }
        this.$refs.policyFormRef.show({type:'ADD',currentNode:this.currentNode})
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const favouredPolicyId = row.favouredPolicyId || this.ids
        this.$refs.policyFormRef.show({type:'UPDATE',favouredPolicyId})
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const favouredPolicyIds = row.favouredPolicyId || this.ids;
        this.$confirm('是否确认删除优惠政策编号为"' + favouredPolicyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFavouredPolicy(favouredPolicyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有优惠政策数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFavouredPolicy(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },
      handleNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id
        this.getList()
      },
      handleFirstNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id;
        this.getList();
      },
      getNormNames(row){
        let names = [];
        for(let i in  row.favouredNorms){
          let confs = row.favouredNorms[i]
          names.push(confs.normNames)
        }
        return names.join(",")
      },
      handleStart(row){
        this.$confirm('是否确认启用此项优惠政策?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return updateStatus({status:'102',favouredPolicyId:row.favouredPolicyId})
        }).then(response => {
          this.msgSuccess("启用成功");
          this.getList()
        }).catch(() => {});
      },
      handleEnd(row){
        this.$confirm('是否确认禁用此项优惠政策?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return updateStatus({status:'103',favouredPolicyId:row.favouredPolicyId})
        }).then(response => {
          this.msgSuccess("禁用成功");
          this.getList()
        }).catch(() => {});
      }
    }
  };
</script>
